Editor.Init = {
	state: function () {
		Editor.STATE.CANVAS = Raphael("canvas", Editor.CONFIG.CANVAS_WIDTH, Editor.CONFIG.CANVAS_HEIGHT);
		Editor.STATE.DRAGGERCANVAS = new Editor.Draggers.Canvas();
		Editor.STATE.DRAGGERCANVAS.draw();
		Editor.STATE.CMDSTACK = new JS.Command.Stack();
		Editor.Util.addSubscribeToCmdStack(Editor.STATE.CMDSTACK);
		Editor.STATE.DRAGGER = undefined;
		Editor.STATE.ADDSHAPE = undefined;
		Editor.STATE.LANG = Editor.LANG.EN;
		Editor.STATE.SHAPES = new JS.Hash();
		Editor.STATE.CONNECTIONS = new JS.Hash();
		Editor.STATE.SELECTED = new Array();
	},
	lang: function () {
		document.title = Editor.STATE.LANG.TITLE;
		$("#logo").text(Editor.STATE.LANG.TITLE); 
		$("#buttonUndo").text(Editor.STATE.LANG.UNDO); 
		$("#buttonRedo").text(Editor.STATE.LANG.REDO); 
		$("#buttonAddResource").text(Editor.STATE.LANG.ADD_RESOURCE); 
		$("#buttonAddLiteral").text(Editor.STATE.LANG.ADD_LITERAL); 
		$("#buttonAddClass").text(Editor.STATE.LANG.ADD_CLASS); 
		$("#buttonAddProperty").text(Editor.STATE.LANG.ADD_PROPERTY); 
		$("#buttonAddIndividual").text(Editor.STATE.LANG.ADD_INDIVIDUAL); 
		$("#buttonDeleteSelected").text(Editor.STATE.LANG.DELETE_SELECTED); 
		$("#buttonToggleLangUi").text(Editor.STATE.LANG.TOGGLE_LANG_UI); 
		$("#buttonRefreshPanel").text(Editor.STATE.LANG.REFRESH_PANEL); 
		$("#buttonSendAddResource").text(Editor.STATE.LANG.SEND); 
		$("#buttonCloseAddResource").text(Editor.STATE.LANG.CLOSE); 
		$("#buttonSendAddLiteral").text(Editor.STATE.LANG.SEND); 
		$("#buttonCloseAddLiteral").text(Editor.STATE.LANG.CLOSE); 
		$("#buttonSendAddClass").text(Editor.STATE.LANG.SEND); 
		$("#buttonCloseAddClass").text(Editor.STATE.LANG.CLOSE); 
		$("#buttonSendImport").text(Editor.STATE.LANG.SEND); 
		$("#buttonCloseImport").text(Editor.STATE.LANG.CLOSE); 
		$("#buttonCloseExport").text(Editor.STATE.LANG.CLOSE); 
		$("#buttonNew").text(Editor.STATE.LANG.NEW_DIAGRAM); 
		$("#buttonExport").text(Editor.STATE.LANG.EXPORT_DIAGRAM); 
		$("#buttonImport").text(Editor.STATE.LANG.IMPORT_DIAGRAM); 
		$("#buttonOptions").text(Editor.STATE.LANG.OPTIONS); 
		$("#buttonCloseOptions").text(Editor.STATE.LANG.CLOSE); 
		$("#buttonSendOptions").text(Editor.STATE.LANG.SEND); 
		$("#buttonAbout").text(Editor.STATE.LANG.ABOUT);
		$("#titleAddResource").text(Editor.STATE.LANG.RESOURCE); 
		$("#titleAddLiteral").text(Editor.STATE.LANG.LITERAL); 
		$("#titleAddVocabClass").text(Editor.STATE.LANG.CLASS); 
		$("#titleAddVocabProperty").text(Editor.STATE.LANG.PROPERTY); 
		$("#titleAddVocabIndividual").text(Editor.STATE.LANG.INDIVIDUAL); 
		$("#titleImport").text(Editor.STATE.LANG.IMPORT_DIAGRAM); 
		$("#titleExport").text(Editor.STATE.LANG.EXPORT_DIAGRAM); 
		$("#buttonTextImportClear").text(Editor.STATE.LANG.CLEAR); 
	},
	buttons: function () {
		$('#buttonUndo').click(Editor.Util.undo);
		$('#buttonRedo').click(Editor.Util.redo);
		$('#buttonAddResource').click(Editor.Util.prepareAddResource);
		$('#buttonAddLiteral').click(Editor.Util.prepareAddLiteral);
		$('#buttonAddVocabClass').click(Editor.Util.prepareAddVocabClass);
		$('#buttonAddVocabProperty').click(Editor.Util.prepareAddVocabProperty);
		$('#buttonAddVocabIndividual').click(Editor.Util.prepareAddVocabIndividual);
		$('#buttonToggleLangUi').click(Editor.Util.toggleLang);
		$('#buttonSendAddLiteral').click(Editor.Util.updateLiteral);
		$('#buttonCloseAddLiteral').click(Editor.Util.cancelUpdateLiteral);
		$('#buttonSendAddResource').click(Editor.Util.updateResource);
		$('#buttonCloseAddResource').click(Editor.Util.cancelUpdateResource);
		$('#buttonSendAddVocabClass').click(Editor.Util.updateVocabClass);
		$('#buttonCloseAddVocabClass').click(Editor.Util.cancelUpdateVocabClass);
		$('#buttonSendAddVocabProperty').click(Editor.Util.updateVocabProperty);
		$('#buttonCloseAddVocabProperty').click(Editor.Util.cancelUpdateVocabProperty);
		$('#buttonSendAddVocabIndividual').click(Editor.Util.updateVocabIndividual);
		$('#buttonCloseAddVocabIndividual').click(Editor.Util.cancelUpdateVocabIndividual);
		$('#buttonDeleteSelected').click(Editor.Util.removeShapes);
		$('#buttonImport').click(Editor.File.prepareImportDiagram);
		$('#buttonSendImport').click(Editor.File.importDiagram);
		$('#buttonExport').click(Editor.File.exportDiagram);
		$('#buttonCloseExport').click(Editor.File.closeExport);
		$('#buttonCloseImport').click(Editor.File.closeImport);
		$('#buttonNew').click(Editor.File.newDiagram);
		$('#buttonClear').click(Editor.File.clearDiagram);
		$('#buttonOptions').click(Editor.Util.prepareOptions);
		$('#buttonSendOptions').click(Editor.Util.updateOptions);
		$('#buttonCloseOptions').click(Editor.Util.closeOptions);
		$('#buttonAbout').click(Editor.Util.about);
		$('.buttonCloseAbout').click(Editor.Util.closeAbout);
		$("#buttonTextImportClear").click(Editor.File.textImportClear);
		$("#buttonUpdNs").click(Editor.Util.updateNamespaces);
		$('#canvasAdd').click(Editor.Util.canvasAddClick);
		$('#buttonToggleCommandHistory').click(Editor.Util.toggleCmdHistoryPanel);
		$('#buttonToggleNanespaces').click(Editor.Util.toggleNamespacesPanel);
		$('#buttonPng').click(Editor.Util.exportToPng);
	},
	ui: function () {
		Editor.Init.lang();
		Editor.Init.buttons();
		Editor.Keys.bind(); 
		$('#canvas').bind('mousemove', function (e) {
			if (Editor.Util.isDragging()) {
				Editor.STATE.DRAGGER.onmousemove(e);
			}
		});
		$('#canvas').bind('mouseup', function () {
			if (Editor.Util.isDragging()) {
				Editor.STATE.DRAGGER.onmouseup();
			}
		}); 
	},
	open: function () {
		var sample = Editor.Util.gup('sample');
		if (sample) {
			var url = 'samples/' + sample + '.json';
			var arrObjects;
 			$.ajax({
				url: url,
				type: "GET",
				dataType: "json",
				cache: false,
				async: false,
				success: function(data) {
					arrObjects = data;
				}
			});
			Editor.Util.drawObjects(arrObjects);

			Editor.STATE.SHAPES.forEach(function(pair) {
				var shape = pair.value;
				if (!shape.getIsDeleted()) {
					shape.updateText();
				}
			});

			delete arrObjects;
		} else {
			//Editor.Util.about();
		}
	},
	init: function () {
		Editor.CONFIG.CANVAS_HEIGHT = $(window).height()-$("#canvas").position().top;
		Editor.CONFIG.CANVAS_WIDTH = $(window).width()-$("#canvas").position().left;

		Editor.Init.state();
		Editor.Init.ui();
		Editor.Init.open();
		$(window).resize(function() {
  			console.log('Handler for .resize() called.');
			Editor.CONFIG.CANVAS_HEIGHT = $(window).height()-$("#canvas").position().top;
			Editor.CONFIG.CANVAS_WIDTH = $(window).width()-$("#canvas").position().left;
			$('#textCanvasWidthUp').val(Editor.CONFIG.CANVAS_WIDTH);
			$('#textCanvasHeightUp').val(Editor.CONFIG.CANVAS_HEIGHT);
		        Editor.STATE.CANVAS.setSize(Editor.CONFIG.CANVAS_WIDTH, Editor.CONFIG.CANVAS_HEIGHT);
            		Editor.STATE.DRAGGERCANVAS.redraw();
		});

       //$().mousemove(function(e){
          //$('#status').html( 'w: ' + $(window).width() + 'x' + $(window).height() + ' m: ' + e.pageX +', '+ (e.pageY - $("#canvas").position().top));
          //$('#status').html( e.pageX +', '+ (e.pageY - $("#canvas").position().top));
       //}); 

		$('#textCanvasWidthUp').val(Editor.CONFIG.CANVAS_WIDTH);
		$('#textCanvasHeightUp').val(Editor.CONFIG.CANVAS_HEIGHT);

		window.onbeforeunload = function (e) {
			var stack = Editor.STATE.CMDSTACK;
                	if (stack.length != 0 || stack.pointer != 0) {
				var e = e || window.event;
				var msg = 'Are you sure you want to leave?';
				// For IE and Firefox
				if (e) {
					e.returnValue = msg;
				}
				// For Safari / chrome
				return msg;
			}
		}
	}
}
